package org.kuan.lab.common.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.kuan.lab.base.util.AppAttributes;
import org.kuan.lab.common.entity.Dict;
import org.kuan.lab.common.service.AppService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Project: erp<br>
 * Description: 应用惟一bean，用于公共信息存储<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
@Controller @Scope("singleton")
public class AppInfo implements Serializable {

	private static final long serialVersionUID = 1L;
	@Resource
	private AppService appService;

	private List<Dict> dicts = new ArrayList<Dict>();
	
	@PostConstruct public void initAppInfo() {
		getAllDicts();
	}
	
	public void getAllDicts() {
		dicts = appService.getAllDicts();
	}
	
	/**
	 * 根据给定的value值，返回名称
	 * @param value
	 * @return
	 */
	public String getNameByDictcode(String code) {
		for(Dict d : dicts) {
			if(d.getCode().equals(code)) {
				return d.getName();
			}
		}
		return null;
	}
	/**
	 * 由根值获取所有的一类字典信息集合
	 * @param name
	 * @return
	 */
	public List<Dict> getDictsByRootcode(String code) {
		List<Dict> ds = new ArrayList<Dict>();
		for(Dict d : dicts) {
			if(d.getCode().matches("^" + code + ".*") && !d.getType().equals(AppAttributes.ERP_DICT_TYPE_PARENT)) {
				ds.add(d);
			}
		}
		return ds;
	}
	
	public AppService getAppService() {
		return appService;
	}

	public void setAppService(AppService appService) {
		this.appService = appService;
	}

}
